# /etc/profile
# Written for Beyond Linux From Scratch
# by James Robertson <jameswrobertson@earthlink.net>
# modifications by Dagmar d'Surreal <rivyqntzne@pbzpnfg.arg>
# modifications by zy.sunshine <zy.netsec@gmail.com>

# System wide environment variables and startup programs should go into
# /etc/profile.

# System wide aliases and functions should go in /etc/bashrc. 
# Personal environment variables and startup programs should go into
# ~/.bash_profile. 
# Personal aliases and functions should go into ~/.bashrc.

pathremove () {
    local IFS=':'
    local NEWPATH
    local DIR
    local PATHVARIABLE=${2:-PATH}
    for DIR in ${!PATHVARIABLE} ; do
        if [ "$DIR" != "$1" ] ; then
            NEWPATH=${NEWPATH:+$NEWPATH:}$DIR
        fi
    done
    export $PATHVARIABLE="$NEWPATH"

}

pathprepend () {
    pathremove $1 $2
    local PATHVARIABLE=${2:-PATH}
    export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}"
}

pathappend () {
    pathremove $1 $2
    local PATHVARIABLE=${2:-PATH}
    export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1"

}

# Add a global uid variable.
if [ -z "$EUID" -a -x /usr/bin/id ] ; then
    EUID=`id -u`
    export EUID
fi

# Set the initial path
export PATH=/bin:/usr/bin
if [ $EUID -eq 0 ] ; then
    pathappend /sbin:/usr/sbin
    unset HISTFILE
fi

# Setup some environment variables(about the command history).
export HISTSIZE=1000
export HISTIGNORE="&:[bf]g:exit"

# Setup a red prompt for root and a green one for users.
NORMAL="\[\e[0m\]"
RED="\[\e[1;31m\]"
GREEN="\[\e[1;32m\]"
if [[ $EUID == 0 ]] ; then
    PS1="$RED\u [ $NORMAL\w$RED ]# $NORMAL"
else
    PS1="$GREEN\u [ $NORMAL\w$GREEN ]\$ $NORMAL"
fi

for script in /etc/profile.d/*.sh ; do
    if [ -r $script ] ; then
        . $script
    fi
done

# Now to clean up
unset pathremove pathprepend pathappend

# I don not know why this bash not excute ~/.bash_profile automatically.
# So run it. If you find bash can run ~/.bash_profile itself, disable it.
if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
fi

